package com.thor.palmmall.rest.hdmall.sales.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.UCN;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.hdmall.common.HDMQueryResult;
import com.thor.palmmall.hdmall.common.HDMQueryResultConverter;
import com.thor.palmmall.hdmall.payment.HDMPaymentService;
import com.thor.palmmall.hdmall.sales.HDMBill;
import com.thor.palmmall.hdmall.sales.HDMSalesInput;
import com.thor.palmmall.hdmall.sales.HDMSalesInputService;
import com.thor.palmmall.hdmall.sales.HDMSalesTotalData;
import com.thor.palmmall.rest.hdmall.sales.MSalesInput;
import com.thor.palmmall.rest.hdmall.sales.MSalesInputCondition;
import com.thor.palmmall.rest.hdmall.sales.MSalesInputQueryParam;
import com.thor.palmmall.rest.hdmall.sales.MSalesInputService;
import com.thor.palmmall.rest.hdmall.sales.MSalesTotalData;
import com.thor.palmmall.rest.hdmall.sales.converter.HDMSalesInputConditionConverter;
import com.thor.palmmall.rest.hdmall.sales.converter.HDMSalesInputConverter;
import com.thor.palmmall.rest.hdmall.sales.converter.HDMSalesInputQueryParamConverter;
import com.thor.palmmall.rest.hdmall.sales.converter.MSalesInputConverter;
import com.thor.palmmall.rest.hdmall.sales.converter.MSalesTotalDataConverter;

/**
 * @author:liyang
 * @time:2017年7月25日 下午4:15:07
 * @since:1.0
 */
public class MSalesInputServiceImpl implements MSalesInputService {

  @Autowired
  HDMSalesInputService hdmSalesInputService;

  @Autowired
  HDMPaymentService hdmPaymentService;

  @Override
  public String save(MSalesInput salesInput) throws ThorServiceException {

    Assert.assertArgumentNotNull(salesInput, "salesInput");

    HDMBill bill = hdmSalesInputService
        .save(HDMSalesInputConverter.getInstance().convert(salesInput));

    if (bill != null) {
      return bill.getNum();
    }
    return null;
  }

  @Override
  public MSalesInput get(String id) throws ThorServiceException {
    Assert.assertArgumentNotNull(id, "id");

    HDMSalesInput salesInput = hdmSalesInputService.get(id);
    return MSalesInputConverter.getInstance().convert(salesInput);

  }

  @Override
  public QueryResult<MSalesInput> query(MSalesInputQueryParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(param, "param");

    HDMQueryResult<HDMSalesInput> rs = hdmSalesInputService
        .query(HDMSalesInputQueryParamConverter.getInstance().convert(param));

    return HDMQueryResultConverter.convert(rs, MSalesInputConverter.getInstance());

  }

  @Override
  public List<MSalesTotalData> getSalesTotal(MSalesInputCondition condition)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(condition, "condition");

    List<HDMSalesTotalData> hdmSalesTotalData = hdmSalesInputService
        .getSalesTotal(HDMSalesInputConditionConverter.getInstance().convert(condition));

    List<MSalesTotalData> salesTotalData = ConverterUtil.convert(hdmSalesTotalData,
        MSalesTotalDataConverter.getInstance());

    if (salesTotalData == null) {
      return new ArrayList<MSalesTotalData>();
    }
    return salesTotalData;

  }

  @Override
  public List<UCN> getAllPayments() throws ThorServiceException {
    return hdmPaymentService.queryPayment();
  }

}
